﻿using HeinQi.Framework.LayerBases;
using PagedList;
using TbShop.Data.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TbShop.Entity;
using HeinQi.Framework.DataAccess;
using System.Data;
using System.Data.SqlClient;

namespace TbShop.Business.Components
{
    public class ServiceComponents<T> : DataComponentsBase<T>, IBusinessContract<T> where T : DataEntityBase
    {
        protected TbShopDBContext CurrentDBContext { set; get; }

        public ServiceComponents()
        {
            this.CurrentDBContext = DBContext;
        }

        public IPagedList<T> GetPagedList(int pageIndex, int pageSize)
        {
            return GetAll().OrderByDescending(x => x.UpdatedDate).ToPagedList(pageIndex, pageSize);
        }
        
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="procedureName"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<TEntity> CallProcedure<TEntity>(string procedureName, DynamicParameters param) where TEntity : class
        {
            var result = base.DBContext.Database.Connection.Query<TEntity>(
                procedureName,
                commandType: System.Data.CommandType.StoredProcedure,
                param: param);
            return result;
        }


        /// <summary>
        /// 执行多集合存储过程
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="procedureName"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public SqlMapper.GridReader CallProcedure(string procedureName, DynamicParameters param)
        {
            var result = base.DBContext.Database.Connection.QueryMultiple(
                procedureName,
                commandType: System.Data.CommandType.StoredProcedure,
                param: param);
            return result;
        }

        /// <summary>
        /// 执行多存储过程获取DataSet
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public DataSet GetDataSetByProcedure(string procedureName, params SqlParameter[] sqlParameters)
        {
            List<string> paras = new List<string>();
            for (int i = 0; i < sqlParameters.Length; i++)
            {
                SqlParameter p = sqlParameters[i];
                paras.Add(p.ParameterName);
            }
            DataSet dataSet = new DataSet();
            SqlConnection conn = this.DBContext.Database.Connection as SqlConnection;
            using (SqlCommand cmd = new SqlCommand(procedureName + " " + string.Join(",", paras.ToArray()), conn))
            {
                cmd.Parameters.AddRange(sqlParameters);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dataSet);
            }
            return dataSet;
        }
        
        /// <summary>
        /// 执行存储过程，获取受影响行数
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public int CallProcedureScale(string procedureName, params SqlParameter[] sqlParameters)
        {
            List<string> paras = new List<string>();
            for (int i = 0; i < sqlParameters.Length; i++)
            {
                SqlParameter p = sqlParameters[i];
                paras.Add(p.ParameterName);
            }
            DataSet dataSet = new DataSet();
            SqlConnection conn = this.DBContext.Database.Connection as SqlConnection;
            using (SqlCommand cmd = new SqlCommand(procedureName + " " + string.Join(",", paras.ToArray()), conn))
            {
                cmd.Parameters.AddRange(sqlParameters);
                conn.Open();
                var count = cmd.ExecuteNonQuery();
                if (conn != null)
                {
                    conn.Close();
                }
                return count;
            }
        }
    }
}
